/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.castorflex.ohos.verticalviewpager.sample;

import fr.castorflex.ohos.verticalviewpager.sample.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据源，每个页面对应list中的一项
 *
 * @author name
 * @since 2021-04-21
 */
public class PagerProvider extends PageSliderProvider {
    private ArrayList<Component> mComponents = new ArrayList<>();
    private ArrayList<Component> mPageViewList;

    /**
     * 创建PageSlide适配器
     *
     * @param pageViewList 页面对应list
     */
    public PagerProvider(ArrayList<Component> pageViewList) {
        this.mPageViewList = pageViewList;
    }

    @Override
    public int getCount() {
        return mPageViewList.size();
    }

    /**
     * 获取Child 没有
     *
     * @param componentContainer componentContainer
     * @param index index
     * @return Object
     */
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int index) {
        Component component = mPageViewList.get(index);
        Text label = (Text) component.findComponentById(ResourceTable.Id_textview);
        componentContainer.addComponent(component);
        mComponents.add(index, label);
        return label;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int index, Object object) {
        componentContainer.removeComponent((Component) object);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    /**
     * Get components
     *
     * @return Components
     */
    public List<Component> getComponents() {
        return mComponents;
    }
}
